"""
-------------------------------
- File_Name: login.py
- Date: 2025/2/18
- Author: yangzhide
- Email: xxzjyzd@126.com
- Description: 系统核心数据表模型
-------------------------------
"""

from tortoise.models import Model
from tortoise import fields
from app.models.utils import *
from app.models.utils import MethodType, BaseModel, OnlineType


class User(BaseModel,TimestampMixin):
    """系统用户表"""
    id = fields.IntField(pk=True, description="用户ID")
    user_code = fields.CharField(max_length=5, unique=True, description="用户编号")
    user_name = fields.CharField(max_length=20, description="用户名称")
    password = fields.CharField(max_length=128, description="密码")
    nick_name = fields.CharField(max_length=30, null=True, description="昵称")
    user_gender = fields.CharEnumField(enum_type=GenderType, default=GenderType.unknow, description="性别")
    user_email = fields.CharField(max_length=255, null=True, description="邮箱")
    user_phone = fields.CharField(max_length=20, null=True, description="电话")
    yez_id = fields.CharField(max_length=1000, null=True,description="授权业主")
    last_login = fields.DatetimeField(null=True, description="最后登录时间")
    status = fields.CharEnumField(enum_type=StatusType, null=True, default=StatusType.disable, description="用户状态")
    online = fields.CharEnumField(enum_type=OnlineType, null=True, default=OnlineType.disable, description="在线状态")
    create_by = fields.CharField(max_length=20,null=True, description="创建人")
    update_by = fields.CharField(max_length=20,null=True, description="更新人")

    class Meta:
        table = "users"
        orm_mode = True

    def __str__(self):
        """用户友好，打印输出简洁易读的字符串"""
        return self.user_name

    def __repr__(self):
        """开发者调试，调用输出精确的字符串"""
        return f"<Student:,{self.user_name}>"

class Role(BaseModel,TimestampMixin):
    id = fields.IntField(pk=True, description="角色ID")
    role_name = fields.CharField(max_length=20, unique=True, description="角色名称")
    role_code = fields.CharField(max_length=20, unique=True, description="角色编码")
    role_desc = fields.CharField(max_length=500, null=True, blank=True, description="角色描述")
    role_home = fields.CharField(default="home", null=True, max_length=100, description="角色首页")
    status = fields.CharEnumField(enum_type=StatusType, default=StatusType.enable, description="状态")
    create_by = fields.CharField(max_length=20, null=True, description="创建人")
    update_by = fields.CharField(max_length=20, null=True, description="更新人")

    class Meta:
        table = "roles"

    def __str__(self):
        return self.role_home
    def __repr__(self):
        return f"<Role:,{self.role_home}>"


class Menu(BaseModel, TimestampMixin):
    id = fields.IntField(pk=True, description="菜单ID")
    menu_name = fields.CharField(max_length=100, description="菜单名称")
    menu_type = fields.CharEnumField(MenuType, description="菜单类型")
    route_name = fields.CharField(max_length=100, description="路由名称")
    route_path = fields.CharField(max_length=200, description="路由路径")
    path_param = fields.CharField(null=True, max_length=200, description="路径参数")
    route_param = fields.JSONField(null=True, description="路由参数, List[dict]")
    order = fields.IntField(default=0, description="菜单顺序")
    component = fields.CharField(null=True, max_length=100, description="路由组件")
    parent_id = fields.IntField(default=0, max_length=10, description="父菜单ID")
    i18n_key = fields.CharField(max_length=100, description="用于国际化的展示文本，优先级高于title")
    icon = fields.CharField(null=True, max_length=100, description="图标名称")
    icon_type = fields.CharEnumField(IconType, null=True, description="图标类型")
    href = fields.CharField(null=True, max_length=200, description="外链")
    multi_tab = fields.BooleanField(default=False, description="是否支持多页签")
    keep_alive = fields.BooleanField(default=False, description="是否缓存")
    hide_in_menu = fields.BooleanField(default=False, description="是否在菜单隐藏")
    active_menu = fields.CharField(null=True, max_length=100, description="隐藏的路由需要激活的菜单")  # 枚举
    fixed_index_in_tab = fields.IntField(null=True, max_length=10, description="固定在页签的序号")
    status = fields.CharEnumField(enum_type=StatusType, default=StatusType.enable, description="状态")
    redirect = fields.CharField(null=True, max_length=200, description="重定向路径")
    props = fields.BooleanField(default=False, description="是否为首路由")
    constant = fields.BooleanField(default=False, description="是否为公共路由")
    create_by = fields.CharField(max_length=20, null=True, description="创建者")
    update_by = fields.CharField(max_length=20, null=True, description="更新者")

    class Meta:
        table = "menus"
        orm_mode = True

    def __str__(self):
        return self.menu_name
    def __repr__(self):
        return f"<Menu:,{self.menu_name}>"


class Button(BaseModel, TimestampMixin):
    id = fields.IntField(pk=True, description="按钮ID")
    menu_id = fields.IntField(null=False,description="菜单ID")
    button_code = fields.CharField(max_length=200, description="按钮编码")
    button_desc = fields.CharField(max_length=200, description="按钮描述")
    status = fields.CharEnumField(enum_type=StatusType, default=StatusType.enable, description="状态")

    class Meta:
        table = "buttons"

    def __str__(self):
        return self.button_desc
    def __repr__(self):
        return f"<Menu:,{self.button_desc}>"


class RoleMenu(BaseModel, TimestampMixin):
    id = fields.IntField(pk=True, description="角色菜单ID")
    role_id = fields.IntField(null=False, description="角色ID")
    menu_id = fields.IntField(null=False, description="菜单ID")

    class Meta:
        # 设置联合唯一约束 👇
        unique_together = ("role_id", "menu_id")
        table = "role_menu"
        table_description = "角色-菜单关联表"

class RoleButton(BaseModel, TimestampMixin):
    id = fields.IntField(pk=True, description="角色按钮ID")
    role_id = fields.IntField(null=False, description="角色ID")
    button_id = fields.IntField(null=False, description="按钮ID")

    class Meta:
        # 设置联合唯一约束
        unique_together = ("role_id", "button_id")
        table = "role_button"
        table_description = "角色-按钮关联表"


class UserRole(BaseModel, TimestampMixin):
    id = fields.IntField(pk=True, description="用户角色ID")
    user_id = fields.IntField(null=False, description="用户ID")
    role_id = fields.IntField(null=False, description="角色ID")

    class Meta:
        # 设置联合唯一约束
        unique_together = ("user_id", "role_id")
        table = "user_role"
        table_description = "用户-角色关联表"


class Api(BaseModel, TimestampMixin):
    """系统api列表"""
    """项目Api接口列表，项目启动后自动更新"""
    id = fields.IntField(pk=True, description="API ID")
    path = fields.CharField(max_length=100, description="API路径")
    method = fields.CharEnumField(MethodType, description="请求方法")
    summary = fields.CharField(max_length=500, null=True, description="请求简介")
    tags = fields.JSONField(max_length=500,null=True, description="API标签")
    status = fields.CharEnumField(enum_type=StatusType, default=StatusType.enable, description="状态")

    class Meta:
        table = "apis"


class APILog(BaseModel):
    """系统api日志列表"""
    id = fields.IntField(pk=True, description="API日志ID")
    user_code = fields.CharField(max_length=50,description="请求者编号")
    user_name = fields.CharField(max_length=50,description="请求者名称")
    method = fields.CharField(max_length=20,null=True, description="请求方法")
    summary = fields.CharField(max_length=500,null=True, description="请求简介")
    ip_address = fields.CharField(max_length=60, description="IP地址")
    request_url = fields.CharField(max_length=255, description="请求URL")
    request_params = fields.JSONField(null=True, description="请求参数")
    request_data = fields.JSONField(null=True, description="请求数据")
    response_data = fields.JSONField(null=True, description="响应数据")
    create_time = fields.DatetimeField(auto_now_add=True, description="创建时间")
    process_time = fields.CharField(max_length=20, null=True, description="请求处理时间")

    class Meta:
        table = "api_logs"

